package com.fbty.mt.dwr;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.directwebremoting.WebContextFactory;


import com.fbty.base.domain.Page;
import com.fbty.base.dwr.DwrAuthHelper;
import com.fbty.mt.domain.Recharge;
import com.fbty.mt.domain.User;
import com.fbty.mt.service.RechargeService;
import com.fbty.mt.service.UserService;

public class RechargeDwr {
	private RechargeService rechargeService;
	/**
	 * 保存购买记录
	 * @param num
	 * @param price
	 * @param time
	 */
	public void buy(Integer num,Double price,Integer time,String roleId) {
		Recharge recharge = new Recharge();
		recharge.setDate(new Date());
		recharge.setNum(num);
		recharge.setPrice(price);
		recharge.setTime(time);
		User user = DwrAuthHelper.getUser();
		recharge.setUid(user.getId());
		user.setNum(user.getNum()+num);
		DwrAuthHelper.getSession().setAttribute("num",user.getNum());
		rechargeService.saveRecharge(recharge, user);
		if("1".equals(roleId)) {
			DwrAuthHelper.getSession().setAttribute("info", " 管理员 ");
		}if("2".equals(roleId)) {
			DwrAuthHelper.getSession().setAttribute("info", "<span style='color:green'>"+user.getUsername()+"</span> | 普通会员 | 您目前还剩下<span style='color:red'>"+user.getNum()+"</span>条购买数量 | ");
		} if("3".equals(roleId)) {
			DwrAuthHelper.getSession().setAttribute("info", "<span style='color:green'>"+user.getUsername()+"</span> | 包月会员 | 您目前还剩下<span style='color:red'>"+user.getNum()+"</span>天!| ");
		} if("4".equals(roleId)) {
			DwrAuthHelper.getSession().setAttribute("info", "<span style='color:green'>"+user.getUsername()+"</span> | 独享用户 | 您目前还剩下<span style='color:red'>"+user.getNum()+"</span>天! | ");
		}
	}
	/**
	 * 显示价格
	 * @param pids
	 * @return
	 */
	public List showPrice() {
		String roleId =  WebContextFactory.get().getSession().getAttribute("roleId").toString();
		return rechargeService.showPrice(roleId);
	}
	/**
	 * 分页显示购买记录
	 * @param pids
	 * @return
	 */
	public Page pageList(Integer pageNo,Map<String,String> parms) {
		//return rechargeService.pageList(pageNo, Page.PRODUCT_PAGE_SIZE, parms);
	return null;
	}
	/**
	 * 删除购买记录
	 * @param pids
	 * @return
	 */
	public void delRecharge(String ids) {
		String[] arr = ids.split(":");
		for(String id : arr) {
			rechargeService.deleteRecharge(id);
		}
	}
	public RechargeService getRechargeService() {
		return rechargeService;
	}

	public void setRechargeService(RechargeService rechargeService) {
		this.rechargeService = rechargeService;
	}
	
}
